{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# python描述器\n",
    "1. 描述器又称描述符，是一个具有*绑定行为*的对象属性，其属性的访问被__描述符协议__方法覆写。这些方法是\\__get\\__()、 \\__set\\__()和\\__delete\\__()，一个对象中只要包含了这三个方法，就称它为描述符。\n",
    "2. 描述器是一个功能强大、通用的协议。它们是__属性(@property语法糖)、方法、静态方法(@staticmethod语法糖)、类方法(@classmethod语法糖)、super()__背后的实现机制。它们被广泛使用于Python 2.2中用来实现__新式类__。\n",
    "\n",
    "备注：装饰器语法糖，见[python装饰器](python装饰器.ipynb); super,见[python方法解析顺序](python方法解析顺序(MRO).ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dir_A:['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'delx', 'getx', 'setx', 'x']\n",
      "dir_B:['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'x']\n",
      "dir_property:['__class__', '__delattr__', '__delete__', '__doc__', '__format__', '__get__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__set__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'deleter', 'fdel', 'fget', 'fset', 'getter', 'setter']\n",
      "enter x.setter\n",
      "100\n",
      "exit x.setter\n",
      "in x.getter\n",
      "100\n",
      "x.deleter\n"
     ]
    }
   ],
   "source": [
    "#??property,内置描述器的例子\n",
    "#写法一()\n",
    "class A(object):\n",
    "    def getx(self): return self._x\n",
    "    def setx(self, value): self._x = value\n",
    "    def delx(self): del self._x\n",
    "    x = property(getx, setx, delx, \"I'm the 'x' property.\")\n",
    "    \n",
    "#写法二\n",
    "class B(object):\n",
    "    def __init__(self):\n",
    "        self._x = 0\n",
    "        \n",
    "    @property\n",
    "    def x(self):\n",
    "        \"I am the 'x' property.\"\n",
    "        print 'in x.getter'\n",
    "        print self._x\n",
    "        return self._x\n",
    "        \n",
    "    @x.setter\n",
    "    def x(self, value):\n",
    "        self._x = value\n",
    "        print 'enter x.setter'\n",
    "        print self._x\n",
    "        print 'exit x.setter'\n",
    "        \n",
    "    @x.deleter\n",
    "    def x(self):\n",
    "        del self._x\n",
    "        print 'x.deleter'\n",
    "\n",
    "print 'dir_A:{}'.format(dir(A))\n",
    "print 'dir_B:{}'.format(dir(B))\n",
    "print 'dir_property:{}'.format(dir(property))\n",
    "\n",
    "b = B()\n",
    "b.x = 100\n",
    "b.x\n",
    "del b.x"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
